package toDoList.Controllers;

import java.io.*;
import java.util.*;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;

import toDoList.Manager.DataBaseHelper;
import toDoList.Manager.Location;
import toDoList.Manager.LocationItemizedOverlay;
import toDoList.Manager.R;
import toDoList.Models.ToDoItemModel;
import toDoList.Models.UserModel;

public class MapController extends MapActivity implements ControllerInterface {
	
	DataBaseHelper helper;
	SQLiteDatabase db;
	UserModel user;
	ArrayList<ToDoItemModel> toDoItemModels;
	List<Overlay> mapOverlays;
	
	/** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapview);
        
    	helper = new DataBaseHelper(this);
    	db = helper.getReadableDatabase();
    	toDoItemModels = new ArrayList<ToDoItemModel>();
    	((MapView)findViewById(R.id.mapview)).setBuiltInZoomControls(true);
    	((MapView)findViewById(R.id.mapview)).displayZoomControls(true);
    	mapOverlays = ((MapView)findViewById(R.id.mapview)).getOverlays();
        
        String userGUID = this.getIntent().getStringExtra("user");
        user = new UserModel(userGUID);
        
        ArrayList<ToDoItemModel> toDoItems = getToDoItems();
        
        for(ToDoItemModel t: toDoItems)
        {
        		toDoItemModels.add(t);
        		addWayPoint(t);
        }
    }
	
	public void addWayPoint(ToDoItemModel t)
	{
		LocationItemizedOverlay itemizedoverlay;
		
		if(t.isChecked())
		{
	        Drawable drawable = this.getResources().getDrawable(R.drawable.bluedot);
	        itemizedoverlay = new LocationItemizedOverlay(drawable, this);
		}
		else
		{
			 Drawable drawable = this.getResources().getDrawable(R.drawable.reddot);
		     itemizedoverlay = new LocationItemizedOverlay(drawable, this);
		}
	        
	        GeoPoint point = t.getLocation();
	        OverlayItem overlayitem = new OverlayItem(point, t.getName(), t.getDescription());
	        
	        itemizedoverlay.addOverlay(overlayitem);
	        mapOverlays.add(itemizedoverlay);
	}
	
	public ArrayList<ToDoItemModel> getToDoItems()
	{
		ArrayList toDoItems = new ArrayList<ToDoItemModel>();
		
		SQLiteDatabase db = helper.getReadableDatabase();	
		//Get all the to-do items
		Cursor mCursor = db.query("ToDoItem", new String[] {"name", "description", "catGUID", "userGUID", "toDoItemGUID", "checked", "location"}, "userGUID = '" + user.getGUID() + "'", null, null, null, null);
	    if (mCursor != null) {
		      mCursor.moveToFirst();
	    }
	    db.close();
	    
		for(int i = 0; i < mCursor.getCount(); i++)
		{
			if(mCursor.getPosition() < mCursor.getCount() - 1)
			{
					toDoItems.add(new ToDoItemModel(mCursor.getString(mCursor.getColumnIndex("name")), mCursor.getString(mCursor.getColumnIndex("description")), 0, mCursor.getString(mCursor.getColumnIndex("catGUID")), mCursor.getString(mCursor.getColumnIndex("userGUID")), mCursor.getString(mCursor.getColumnIndex("toDoItemGUID")), mCursor.getString(mCursor.getColumnIndex("checked")), mCursor.getString(mCursor.getColumnIndex("location"))));
					mCursor.moveToNext();
			}
			else
			{
					
					toDoItems.add(new ToDoItemModel(mCursor.getString(mCursor.getColumnIndex("name")), mCursor.getString(mCursor.getColumnIndex("description")), 0, mCursor.getString(mCursor.getColumnIndex("catGUID")), mCursor.getString(mCursor.getColumnIndex("userGUID")), mCursor.getString(mCursor.getColumnIndex("toDoItemGUID")), mCursor.getString(mCursor.getColumnIndex("checked")), mCursor.getString(mCursor.getColumnIndex("location"))));
			}
	    }
		
		return toDoItems;
	}
	
	/**
	 * createListener(): Used just to link classes together through the controllerInterface interface
	 */
	public void createListener()
	{
		
	}

	/**
	 * setCurrentLocatin(Location l) Sets the current location on the map
	 * @param l The location to set the map to
	 */
	public void setCurrentLocation(Location l) {
		
	}

	/**
	 * showSettingsView() Shows the settings view
	 */
	public void showSettingsView() {
		throw new UnsupportedOperationException("The method is not implemented yet.");
	}

	/**
	 * popUp(String n) Displays a pop-up window with the message "n" inside of it.
	 * @param n The string to display
	 */
	public void popUp(String n) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(n)
		       .setCancelable(false)
		       .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		           }
		       });
		
		builder.show();
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

}
